CLIP.Search.enc = function(val) {
	return val.replace(/ /g, '+');
};

CLIP.Search.dec = function(val) {
	return val.replace(/\+/g, ' ');
};

CLIP.Clips.initPage = 1;
CLIP.Clips.currentPage = 1;
CLIP.Clips.pageSize = 40;

CLIP.Clips.searchUrl = function(keyword) {
	return 'http://m.tv.zing.vn/ajax?f=searchProgram&q=' + encodeURIComponent(keyword) + '&s=date&page=' + CLIP.Clips.currentPage + '&count=40';
};

CLIP.Clips.filterUrl = function() {
	return 'http://m.tv.zing.vn/ajax?f=genreProgram&count=40&' + $('#filterForm').serialize();
};

CLIP.Clips.moreHtml = function(text) {
	var html = [];
	$.each(text.split('<a class="content-items"'), function(i, clip) {
		if (i > 0) {
			++CLIP.Clips.moreCount;
			var code = bum.util.parse(clip, 'href="/p/', '">');
			var title = bum.util.parse(clip, '<h3>', '</h3>');
			var img = bum.util.parse(clip, 'src="', '"');

			html.push('<a class="item clip" href="#" code="', code, '"><div class="cw">');
			html.push('<img src="http://cdn.bum.vn/_.gif" style="background-image:url(', img, ');"/>');
			html.push('<div class="iw"><h4>', title, '</h4>');
			html.push('<h5></h5></div>');
			html.push('</div></a>');
		}
	});
	return html;
};

CLIP.Info.infoUrl = function(code) {
	return 'http://m.tv.zing.vn/p/' + code;
};

CLIP.Info.parseInfo = function(text) {
	text = bum.util.parse(text, '<!--End .top-head -->', '<!--End .sugguest-->');
	if (text == null) {
		return null;
	}

	var poster = bum.util.parse(text, '<img src="', '"');
	var title = bum.util.parse(text, '<h3>', '</h3>');
	var originalTitle = bum.util.parse(text, '<h4>', '</h4>');
	var intro = bum.util.parse(text, '<p id="description" class="rows2" expand="rows2">', '</p>');

	var clip = {
		poster : poster,
		title : title,
		originalTitle : originalTitle,
		intro : intro
	};

	var url = bum.util.parse(text, 'autoload-src="', '"></div>');
	if (url) {
		bum.api.fetchUrl(url + '&page=1&count=200', CLIP.Chap.headers, CLIP.Info.getSuggests);
	}

	return clip;
};

// zing only
CLIP.Info.getSuggests = function(html) {
	$.each(html.split('<a href="/series/'), function(i, part) {
		if (i > 0) {
			var code = bum.util.parse(part, '/', '.html');
			var url = 'http://m.tv.zing.vn/ajax?f=loadMediaSeries&page=1&count=500&id=' + code;

			bum.api.fetchUrl(url, CLIP.Chap.headers, CLIP.Info.getChaps);
		}
	});
};

// zing only
CLIP.Info.getChaps = function(text) {
	var html = [];
	text = text.replace(/Tập /gi, '');
	$.each(text.split('<a class="content-items" href="/video/'), function(i, part) {
		if (i > 0) {
			var code = bum.util.parse(part, '/', '.html');
			var name = bum.util.parse(part, '<h3>', '</h3>');

			html.push('<a href="' + code + '">' + name + '</a>');
		}
	});

	$('#chaps').append('<div>' + html.reverse().join('') + '</div>');
};


CLIP.Chap.headers = [ 'Accept', 'text/plain, text/html', 'Host', 'm.tv.zing.vn', 'Cache-Control', 'max-age=0', 'Connection', 'close', 'User-Agent',
		'Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' ].join('|');

CLIP.Chap.chapUrl = function(code) {
	return 'http://m.tv.zing.vn/video/a/' + code + '.html';
};
// custome
CLIP.Chap.streamUrl = function(text) {
	return bum.util.parse(text, '<source src="', '"');
};